//
//  JBWStatusViewModel.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/4.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit

class JBWStatusViewModel: NSObject {

    //模型
    var homeModel: JBWHomeModel?{
        didSet{
            //认证
            verifiedImage = dealVerifiedImage(verified: homeModel?.user?.verified ?? -1)
            //会员等级
            mbrankImage = dealMbrankImage(mbrank: homeModel?.user?.mbrank ?? 0)
            //微博来源
            sourceText = dealSinaSource(source: homeModel?.source)
            
        }
    }
    
    //认证图片
    var verifiedImage: UIImage?
    //会员等级
    var mbrankImage: UIImage?
    //微博时间
    var createdAtText: String? {
        return dealSinaTime(created_at: homeModel?.created_at)
    }
    //微博来源
    var sourceText: String?
    
}

extension JBWStatusViewModel {
    
    //用户认证图片
    fileprivate func dealVerifiedImage(verified: Int) -> UIImage?{
        
        //图片名称
        var imgName = ""
        switch verified {
        case 1:
            imgName = "avatar_vip"
        case 2,3,5:
            imgName = "avatar_enterprise_vip"
        case 220:
            imgName = "avatar_grassroot"
        default:
            imgName = "avatar_vgirl"
        }
        return UIImage(named: imgName)
        
    }
    
    //会员等级 1-6
    fileprivate func dealMbrankImage(mbrank: Int) -> UIImage?{
        if mbrank >= 1 && mbrank <= 6 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        return UIImage(named: "common_icon_membership")
    }
}

//处理微博时间
extension JBWStatusViewModel {
    fileprivate func dealSinaTime(created_at: Date?) -> String?{
        //判断是否为nil
        guard let d = created_at else {
            
            return nil
        }
        
        //定义时间格式化
        let df = DateFormatter()
        //定义日期格式字符串
        let dateFormatString: String
        //判断是否是今年
        if isThisYear(sinaDate: d) {
            //日历类
            let calender = Calendar.current
            //如果是今天
            if calender.isDateInToday(d) {
                //计算发布微博日期和当前日期比较差值
                let s = Int(-d.timeIntervalSinceNow)
                //显示刚刚
                if s < 60 {
                    return "刚刚"
                } else if s >= 60 && s < 3600 {
                    return "\(s/60)分钟前"
                } else {
                    return "\(s/3600)小时前"
                }
                
            } else if calender.isDateInYesterday(d) {
                //昨天
                dateFormatString = "昨天 HH:mm"
            } else {
                //05月21日 12:32
                dateFormatString = "MM月dd日 HH:mm"
            }
        } else {
            //2015年05月21日 12:32
            dateFormatString = "yyyy年MM月dd日 HH:mm"
        }
        
        //指定格式
        df.dateFormat = dateFormatString
        //返回
        return df.string(from: d)
    }
    
    //判断是否是今年
    fileprivate func isThisYear(sinaDate: Date) -> Bool{
        
        //定义时间格式化
        let df = DateFormatter()
        //指定格式
        df.dateFormat = "yyyy"
        //拿到系统时间
        let currentDate = Date()
        //通过日期拿到要用的字符串
        let sinaString = df.string(from: sinaDate)
        //当前时间
        let currentString = df.string(from: currentDate)
        return sinaString == currentString
        
    }
}

//处理微博来源
extension JBWStatusViewModel{
    
    fileprivate func dealSinaSource(source: String?) -> String?{
        //判断是否为nil 且是否保存
        guard let s = source, s.contains("\">") else {
            return "来自 Smartisan T3"
        }
        //截取字符
        let startIndex = s.range(of: "\">")!
        let endIndex = s.range(of: "</")
        //
        let resualt = s.substring(with: startIndex.upperBound..<(endIndex?.lowerBound)!)
        return "来自" + resualt
        
    }
}
